﻿using B.S.Data.MES.API.Applications.BaseData.Command.CheJianCommand;
using B.S.Domain.BasicData.Factory_modeling.shop_management;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.BaseData.CommandHandlers.ChejianCommandHandlers
{
    public class ListChenJianCommandHandlers : IRequestHandler<ListChejianCommand, APIRequst<APIPageing<List<shop_management>>>>
    {
        private readonly IBaseRepository<shop_management> shopRepository;

        public ListChenJianCommandHandlers(IBaseRepository<shop_management> shopRepository)
        {
            this.shopRepository = shopRepository;
        }

        public Task<APIRequst<APIPageing<List<shop_management>>>> Handle(ListChejianCommand request, CancellationToken cancellationToken)
        {
            APIRequst<APIPageing<List<shop_management>>> requst = new APIRequst<APIPageing<List<shop_management>>>();
            requst.Code = RequstCode.成功;
            requst.Msg = RequstCode.成功.ToString();
            var list = shopRepository.GetAll().Where(x=>x.isDel==false);
            if (request.UpdateTime != null)
            {
                list = list.Where(x => x.UpdateTime == request.UpdateTime);
            }
            if (request.CreateName != null)
            {
                list = list.Where(x => x.CreateName == request.CreateName);
            }
            if (request.CreateTime != null)
            {
                list = list.Where(x => x.CreateName == request.CreateName);
            }
            if (request.Workshopcoding!=null)
            {
                list = list.Where(x => x.Workshopcoding == request.Workshopcoding);
            }
            if (request.Workshopname != null)
            {
                list = list.Where(x => x.Workshopname == request.Workshopname);
            }
            if (request.describe != null)
            {
                list = list.Where(x => x.describe == request.describe);
            }
            var totalCount =list.Count();
            var total = (int)Math.Ceiling(1.0 * totalCount / request.PageSize);
            var listDto = list.OrderBy(x => x.Workid).Skip((request.Pageindex - 1) * request.PageSize).Take(request.PageSize).ToList();
            APIPageing<List<shop_management>> Page = new APIPageing<List<shop_management>>();
            Page.Total=total;
            Page.TotalCount=totalCount;
            Page.Data = listDto;
            requst.Data = Page;
            return Task.FromResult(requst);
        }
    }
}
